/*     */ package com.cr.system.service.impl;
/*     */ 
/*     */ import com.cr.common.annotation.DataScope;
/*     */ import com.cr.common.core.domain.Ztree;
/*     */ import com.cr.common.core.domain.entity.SysDept;
/*     */ import com.cr.common.core.domain.entity.SysRole;
/*     */ import com.cr.common.core.domain.entity.SysUser;
/*     */ import com.cr.common.core.text.Convert;
/*     */ import com.cr.common.exception.ServiceException;
/*     */ import com.cr.common.utils.ShiroUtils;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.common.utils.spring.SpringUtils;
/*     */ import com.cr.system.mapper.SysDeptMapper;
/*     */ import com.cr.system.service.ISysDeptService;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import org.apache.commons.lang3.ArrayUtils;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Service;
/*     */ import org.springframework.transaction.annotation.Transactional;
/*     */ 
/*     */ @Service
/*     */ public class SysDeptServiceImpl implements ISysDeptService {
/*     */   @Autowired
/*     */   private SysDeptMapper deptMapper;
/*     */   
/*     */   @DataScope(deptAlias = "d")
/*     */   public List<SysDept> selectDeptList(SysDept dept) {
/*  45 */     return this.deptMapper.selectDeptList(dept);
/*     */   }
/*     */   
/*     */   @DataScope(deptAlias = "d")
/*     */   public List<Ztree> selectDeptTree(SysDept dept) {
/*  58 */     List<SysDept> deptList = this.deptMapper.selectDeptList(dept);
/*  59 */     List<Ztree> ztrees = initZtree(deptList);
/*  60 */     return ztrees;
/*     */   }
/*     */   
/*     */   @DataScope(deptAlias = "d")
/*     */   public List<Ztree> selectDeptTreeExcludeChild(SysDept dept) {
/*  73 */     Long excludeId = dept.getExcludeId();
/*  74 */     List<SysDept> deptList = this.deptMapper.selectDeptList(dept);
/*  75 */     Iterator<SysDept> it = deptList.iterator();
/*  76 */     while (it.hasNext()) {
/*  78 */       SysDept d = it.next();
/*  79 */       if (d.getDeptId().intValue() == excludeId.longValue() || 
/*  80 */         ArrayUtils.contains((Object[]) MyStringUtils.split(d.getAncestors(), ","), excludeId + ""))
/*  82 */         it.remove(); 
/*     */     } 
/*  85 */     List<Ztree> ztrees = initZtree(deptList);
/*  86 */     return ztrees;
/*     */   }
/*     */   
/*     */   public List<Ztree> roleDeptTreeData(SysRole role) {
/*  98 */     Long roleId = role.getRoleId();
/*  99 */     List<Ztree> ztrees = new ArrayList<>();
/* 100 */     List<SysDept> deptList = selectDeptList(new SysDept());
/* 101 */     if (MyStringUtils.isNotNull(roleId)) {
/* 103 */       List<String> roleDeptList = this.deptMapper.selectRoleDeptTree(roleId);
/* 104 */       ztrees = initZtree(deptList, roleDeptList);
/*     */     } else {
/* 108 */       ztrees = initZtree(deptList);
/*     */     } 
/* 110 */     return ztrees;
/*     */   }
/*     */   
/*     */   public List<Ztree> initZtree(List<SysDept> deptList) {
/* 121 */     return initZtree(deptList, null);
/*     */   }
/*     */   
/*     */   public List<Ztree> initZtree(List<SysDept> deptList, List<String> roleDeptList) {
/* 134 */     List<Ztree> ztrees = new ArrayList<>();
/* 135 */     boolean isCheck = MyStringUtils.isNotNull(roleDeptList);
/* 136 */     for (SysDept dept : deptList) {
/* 138 */       if ("0".equals(dept.getStatus())) {
/* 140 */         Ztree ztree = new Ztree();
/* 141 */         ztree.setId(dept.getDeptId());
/* 142 */         ztree.setpId(dept.getParentId());
/* 143 */         ztree.setName(dept.getDeptName());
/* 144 */         ztree.setTitle(dept.getDeptName());
/* 145 */         if (isCheck)
/* 147 */           ztree.setChecked(roleDeptList.contains(dept.getDeptId() + dept.getDeptName())); 
/* 149 */         ztrees.add(ztree);
/*     */       } 
/*     */     } 
/* 152 */     return ztrees;
/*     */   }
/*     */   
/*     */   public int selectDeptCount(Long parentId) {
/* 164 */     SysDept dept = new SysDept();
/* 165 */     dept.setParentId(parentId);
/* 166 */     return this.deptMapper.selectDeptCount(dept);
/*     */   }
/*     */   
/*     */   public boolean checkDeptExistUser(Long deptId) {
/* 178 */     int result = this.deptMapper.checkDeptExistUser(deptId);
/* 179 */     return (result > 0);
/*     */   }
/*     */   
/*     */   public int deleteDeptById(Long deptId) {
/* 191 */     return this.deptMapper.deleteDeptById(deptId);
/*     */   }
/*     */   
/*     */   public int insertDept(SysDept dept) {
/* 203 */     SysDept info = this.deptMapper.selectDeptById(dept.getParentId());
/* 205 */     if (!"0".equals(info.getStatus()))
/* 207 */       throw new ServiceException("部门停用，不允许新增"); 
/* 209 */     dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
/* 210 */     return this.deptMapper.insertDept(dept);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int updateDept(SysDept dept) {
/* 223 */     SysDept newParentDept = this.deptMapper.selectDeptById(dept.getParentId());
/* 224 */     SysDept oldDept = selectDeptById(dept.getDeptId());
/* 225 */     if (MyStringUtils.isNotNull(newParentDept) && MyStringUtils.isNotNull(oldDept)) {
/* 227 */       String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
/* 228 */       String oldAncestors = oldDept.getAncestors();
/* 229 */       dept.setAncestors(newAncestors);
/* 230 */       updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
/*     */     } 
/* 232 */     int result = this.deptMapper.updateDept(dept);
/* 233 */     if ("0".equals(dept.getStatus()) && MyStringUtils.isNotEmpty(dept.getAncestors()) &&
/* 234 */       !MyStringUtils.equals("0", dept.getAncestors()))
/* 237 */       updateParentDeptStatusNormal(dept); 
/* 239 */     return result;
/*     */   }
/*     */   
/*     */   private void updateParentDeptStatusNormal(SysDept dept) {
/* 249 */     String ancestors = dept.getAncestors();
/* 250 */     Long[] deptIds = Convert.toLongArray(ancestors);
/* 251 */     this.deptMapper.updateDeptStatusNormal(deptIds);
/*     */   }
/*     */   
/*     */   public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
/* 263 */     List<SysDept> children = this.deptMapper.selectChildrenDeptById(deptId);
/* 264 */     for (SysDept child : children)
/* 266 */       child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); 
/* 268 */     if (children.size() > 0)
/* 270 */       this.deptMapper.updateDeptChildren(children); 
/*     */   }
/*     */   
/*     */   public SysDept selectDeptById(Long deptId) {
/* 283 */     return this.deptMapper.selectDeptById(deptId);
/*     */   }
/*     */   
/*     */   public int selectNormalChildrenDeptById(Long deptId) {
/* 295 */     return this.deptMapper.selectNormalChildrenDeptById(deptId);
/*     */   }
/*     */   
/*     */   public String checkDeptNameUnique(SysDept dept) {
/* 307 */     Long deptId = Long.valueOf(MyStringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId().longValue());
/* 308 */     SysDept info = this.deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
/* 309 */     if (MyStringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
/* 311 */       return "1"; 
/* 313 */     return "0";
/*     */   }
/*     */   
/*     */   public void checkDeptDataScope(Long deptId) {
/* 324 */     if (!SysUser.isAdmin(ShiroUtils.getUserId())) {
/* 326 */       SysDept dept = new SysDept();
/* 327 */       dept.setDeptId(deptId);
/* 328 */       List<SysDept> depts = ((SysDeptServiceImpl)SpringUtils.getAopProxy(this)).selectDeptList(dept);
/* 329 */       if (MyStringUtils.isEmpty(depts))
/* 331 */         throw new ServiceException("没有权限访问部门数据！"); 
/*     */     } 
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-system-4.7.0.jar!\com\cr\system\service\impl\SysDeptServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */